package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;


public class RedisDemoString {
    // mset 和 mget
    public static void test01(Jedis jedis) {
        System.out.println("mget 和 mset");
        jedis.flushAll();
        jedis.mset("key1", "key1", "key2", "key2", "key3", "key3");
//        System.out.println(jedis.keys("*"));
        List<String> mget = jedis.mget("key1", "key2");
        System.out.println(mget);
    }

    // setnx 和 setex
    public static void test02(Jedis jedis) {
        System.out.println("setnx 和 setxx");
        // key  不存在，设置成功
        System.out.println(jedis.setnx("key5", "h5"));
        System.out.println(jedis.get("key5"));
        // key 存在，设置失败
        System.out.println(jedis.setnx("key1", "h5"));
        System.out.println(jedis.get("key1"));
        System.out.println("----------------");
        System.out.println(jedis.setex("key6", 10, "h6"));
    }

    // 计数命令
    public static void test03(Jedis jedis) {
        jedis.set("h1", "1");
        System.out.println(jedis.get("h1"));
        System.out.println(jedis.incr("h1"));
//        System.out.println(jedis.incr("key1"));
        System.out.println(jedis.incrBy("h1", 10));
        System.out.println(jedis.incrByFloat("h1", -1.1));
        System.out.println(jedis.incrByFloat("h2", 0.5));
    }

    // append getrange setrange
    public static void test04(Jedis jedis) {
        System.out.println(jedis.append("h1", "abcde"));
        System.out.println(jedis.get("h1"));
        System.out.println(jedis.setrange("h1", 3, "hhhhhhh"));
        System.out.println(jedis.getrange("h1", 4, -1));
    }

    // strlen
    public static void test05(Jedis jedis) {
        System.out.println(jedis.strlen("h1"));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool(Constant.IP);
        try (Jedis jedis = jedisPool.getResource()){
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
//            test04(jedis);
            test05(jedis);
        }
    }
}
